//
//  TF_RotatePictures.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/2.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
//轮播图片
class TF_RotatePictures: UIView {
    @objc public var mNavigationbarHeight:CGFloat = 0
    let modelArray = NSMutableArray()
    let scrollView = UIScrollView()
    let close = UIButton()
    override init(frame: CGRect) {
        super.init(frame: frame)
      
        self.isHidden = true

        self.scrollView.isPagingEnabled = true
        self.addSubview(self.scrollView)
        
        //TODO:退出直播间
        self.close.setImage(UIImage.init(named: "HTLive.bundle/LivingClose"), for: .normal)
        
        self.close.backgroundColor = UIColor.clear
        self.addSubview(self.close)
        self.close.addTarget(self, action:#selector(self.closeClick), for: .touchUpInside)
     
    }
    @objc func closeClick()
    {
        self.isHidden = true
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.scrollView.frame = self.bounds
        
        var livingCloseY:CGFloat = 0
        let livingCloseH:CGFloat = 30
        if (self.isNotchScreen) {
            livingCloseY = self.mNavigationbarHeight;
        }else{
           
            livingCloseY = 15
        }
        self.close.frame = CGRect(x: self.frame.size.width - 30 - 15, y: livingCloseY, width: livingCloseH, height: livingCloseH)

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc public func addPictures(model:TF_LivingChatData)

    {
        self.backgroundColor = UIColor.black
        
        let imageViewWidth:CGFloat  = self.frame.size.width;
        let imageViewX:CGFloat      = imageViewWidth * CGFloat(self.modelArray.count);
        let imageViewHeight:CGFloat = self.frame.size.height
        let imageView  = UIImageView()
       
        imageView.contentMode = .scaleAspectFit;
        imageView.image = model.image;
        imageView.frame = CGRect(x: imageViewX, y: 0, width: imageViewWidth, height: imageViewHeight)
      
        self.scrollView.addSubview(imageView)
        self.modelArray.add(model)
        
        self.scrollView.contentSize = CGSize(width: CGFloat(self.modelArray.count) * imageViewWidth, height: 0)
   
    }
    
    @objc public func setContentOffsetWithModel(model:TF_LivingChatData,animate:Bool)

    {
        for i in stride(from: 0, to:self.modelArray.count ,by: 1) {
            
            let data = self.modelArray[i] as! TF_LivingChatData
            
            if (data.imageURL == model.imageURL) {
                self.scrollView.setContentOffset(CGPoint(x: Int(self.scrollView.frame.size.width)*i, y: 0), animated: animate)
                
                break;
                
            }
            
        }
    }
}
